#include "EditBar.h"
using namespace std;

EditBar::EditBar(Artist& artist)
{
    mode = TERRAIN;
    tool = PENCIL;
    bounds = new SDL_Rect;
    bounds->x = 0;
    bounds->w = WINDOW_WIDTH;
    bounds->h = 96;
    bounds->y = WINDOW_HEIGHT - bounds->h;

    expanded = false;
    bank_select = -1;
    bank_scrolled = 0;

    textColor.r = 0;
    textColor.g = 0;
    textColor.b = 0;

    font = TTF_OpenFont( "jvgasys.fon", 28 );

    title.set_x(70);
    save.set_x(100);
    theme.set_x(70);
    size_w.set_x(70);
    size_h.set_x(70);

    mapname = "mymap";
    themeString = "underwater";

    save.setImage(artist, "system/floppydisk.png", false, false, true);
    pencil.setImage(artist, "system/pencil.png", false, false, true);
    eraser.setImage(artist, "system/eraser.png", false, false, true);
    brush.setImage(artist, "system/brush.png", false, false, true);
    rotation.setImage(artist, "system/rotate.png", false, false, true);
    left_arrow.setImage(artist, "system/left_arrow.png", false, false, true);
    right_arrow.setImage(artist, "system/right_arrow.png", false, false, true);
    highlight.setImage(artist, "system/highlight.png", false, false, true);
}

void EditBar::show(SDL_Surface* screen, string w, string h)
{

    if(expanded && bounds->y > 30)
    {
        bounds->y -= 20;
        bounds->h = WINDOW_HEIGHT - bounds->y;
    }
    else if(expanded && bounds->y > 0)
    {
        bounds->y /= 2;
        bounds->h = WINDOW_HEIGHT - bounds->y;
    }
    if(!expanded && bounds->y < WINDOW_HEIGHT - 106)
    {
        bounds->y += 20;
        bounds->h = WINDOW_HEIGHT - bounds->y;
    }
    else if(!expanded && bounds->y < WINDOW_HEIGHT - 96)
    {
        bounds->y += (WINDOW_HEIGHT - 96 - bounds->y) /2;
        bounds->h = WINDOW_HEIGHT - bounds->y;
    }
    else if(!expanded && bounds->y > WINDOW_HEIGHT - 96)
    {
        bounds->y = WINDOW_HEIGHT - 96;
        bounds->h = WINDOW_HEIGHT - bounds->y;
    }

    int bar_y = bounds->y;
    boxColor(screen, 0, bar_y, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, (Uint32)0xFFFEFEFE);

    for(int i=1; bar_y + i*20 < WINDOW_HEIGHT; i++)
    {
        hlineColor(screen, 0, WINDOW_WIDTH, bar_y + 20*i, (Uint32)0x63c3ebff);
        hlineColor(screen, 0, WINDOW_WIDTH, bar_y + 20*i -1, (Uint32)0xa0d8efaa);
    }
        vlineColor(screen, 50, bar_y, WINDOW_HEIGHT, (Uint32)0xff030099);
        vlineColor(screen, 49, bar_y, WINDOW_HEIGHT, (Uint32)0xff030044);
        vlineColor(screen, WINDOW_WIDTH-55, bar_y, WINDOW_HEIGHT, (Uint32)0xff030099);
        vlineColor(screen, WINDOW_WIDTH-56, bar_y, WINDOW_HEIGHT, (Uint32)0xff030044);

    if(mode == BG_DECORATION || mode == FG_DECORATION)
    {
        left_arrow.display(screen, false, 0, bar_y + 20);
        right_arrow.display(screen, false, WINDOW_WIDTH - right_arrow.get_bounds().w - 2, bar_y + 20);
        for (int i = 0; i < objectBank.size(); i++)
        {
            SDL_Rect pos;
            pos.x = -1 * left_arrow.get_bounds().w;
            pos.y = -1 * (bar_y + 14);
            objectBank[i]->display(pos, screen, true);
        }
    }
    else
    {
        pencil.display(screen, false, 80, bar_y + 5);
        eraser.display(screen, false, 200, bar_y + 15);
        brush.display(screen, false, 305, bar_y + 5);
        rotation.display(screen, false, 455, bar_y + 10);
        switch(tool)
        {
            case PENCIL: highlight.display(screen, false, 70, bar_y); break;
            case ERASER: highlight.display(screen, false, 180, bar_y); break;
            case BRUSH: highlight.display(screen, false, 300, bar_y); break;
            case ROTATION: highlight.display(screen, false, 440, bar_y); break;
        }

    }

    if(bar_y < WINDOW_HEIGHT - 96)
    {
        save.set_y(bar_y+ 120);
        save.display(screen);

        title.set_y(bar_y + 224);
        if(typing && field == NAME)
            title.setSurface(TTF_RenderText_Solid( font, ("Title: " + inputBuffer).c_str(), textColor ));
        title.display(screen);

        theme.set_y(bar_y + 264);
        if(typing && field == THEME)
            theme.setSurface(TTF_RenderText_Solid( font, ("Theme: " + inputBuffer).c_str(), textColor ));
        theme.display(screen);

        size_w.set_y(bar_y + 302);
        size_w.setSurface(TTF_RenderText_Solid( font, ("Width:  " + w).c_str(), textColor ));
        size_w.display(screen);

        size_h.set_y(bar_y + 342);
        size_h.setSurface(TTF_RenderText_Solid( font, ("Height: " + h).c_str(), textColor ));
        size_h.display(screen);
    }
}

void EditBar::cycle(Direction dir)
{
    switch(dir)
    {
        case UP:
        case RIGHT:
                    if(mode == BG_DECORATION)
                        mode = TERRAIN;
                    else if(mode == TERRAIN)
                        mode = FG_DECORATION;
                    else if(mode == FG_DECORATION)
                        mode = BG_DECORATION;
        break;

        case DOWN:
        case LEFT:
                    if(mode == BG_DECORATION)
                        mode = FG_DECORATION;
                    else if(mode == FG_DECORATION)
                        mode = TERRAIN;
                    else if(mode == TERRAIN)
                        mode = BG_DECORATION;
        break;
    }
}
